#include "widget.h"

#include <QGridLayout>
#include <QContextMenuEvent>
#include <QMenu>
#include <QLayoutItem>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle("分屏");
    initWidget();
    this->resize(800,500);
    this->setContextMenuPolicy(Qt::DefaultContextMenu);
}

Widget::~Widget() {}

void Widget::initWidget()
{
    initMenu();
    for(int i =0;i<9;i++)
    {
        QLabel* label = new QLabel;
        label->setStyleSheet(QString("QLabel{background-image:url(:/res/%1.png); \
            border:1px solid gray; \
            background-position:center; \
            background-repeat:no-repeat; \
        }").arg(QString::number(i+1)));
        m_layoutList.append(label);
    }
    switchLayout(SettingLayoutType::OneType);//默认显示1屏
}

void Widget::initMenu()
{
    m_swtichMenu = new QMenu(this);
    m_swtichMenu->addAction("一屏");
    m_swtichMenu->addAction("四屏");
    m_swtichMenu->addAction("五屏");
    m_swtichMenu->addAction("六屏");
    m_swtichMenu->addAction("九屏");

    QMap<QString,int> strTypeMap;
    strTypeMap["一屏"] = SettingLayoutType::OneType;
    strTypeMap["四屏"] = SettingLayoutType::FourType;
    strTypeMap["五屏"] = SettingLayoutType::FiveType;
    strTypeMap["六屏"] = SettingLayoutType::SixType;
    strTypeMap["九屏"] = SettingLayoutType::NineType;

    connect(m_swtichMenu,&QMenu::triggered,this,[=](QAction* action){
        switchLayout(SettingLayoutType(strTypeMap[action->text()]));
    });
}

void Widget::contextMenuEvent(QContextMenuEvent *event)
{
    m_swtichMenu->exec(QCursor::pos());
}

void Widget::switchLayout(SettingLayoutType type)
{
    QLayout* layout = this->layout();
    if(layout){
        //清空原来的
        QLayoutItem* child;
        while((child=layout->takeAt(0)) != 0)
        {
            if(child->widget())
            {
                child->widget()->setParent(NULL);
            }
            delete child;
        }
        delete layout;
    }
    //切换显示页面
    switch (type) {
    case OneType:
    {
        QGridLayout* gLayout = new QGridLayout(this);
        gLayout->addWidget(m_layoutList[0]);
        gLayout->setContentsMargins(0,0,0,0);

    }

        break;
    case FourType:
    {
        QGridLayout* gLayout = new QGridLayout(this);
        gLayout->setSpacing(0);
        gLayout->setContentsMargins(0,0,0,0);

        for(int i=0;i<4;i++)
        {
            gLayout->addWidget(m_layoutList[i],i/2,i%2);
        }
    }
    break;
    case FiveType:
    {
        QVBoxLayout* pVLay = new QVBoxLayout(this);
        pVLay->setSpacing(0);

        QHBoxLayout* pHTopLay = new QHBoxLayout(this);
        pHTopLay->setSpacing(0);
        for (int i = 0; i < 3; i++)
        {
            pHTopLay->addWidget(m_layoutList[i]);
        }

        QHBoxLayout* pHBottomLay = new QHBoxLayout(this);
        pHBottomLay->setSpacing(0);
        for (int i = 3; i < 5; i++)
        {
            pHBottomLay->addWidget(m_layoutList[i]);
        }

        pVLay->addLayout(pHTopLay);
        pVLay->addLayout(pHBottomLay);
    }
    break;
    case SixType:
    {
        QGridLayout* gLayout = new QGridLayout(this);
        gLayout->addWidget(m_layoutList[0], 0, 0, 2, 2);
        gLayout->addWidget(m_layoutList[1], 0, 2);
        gLayout->addWidget(m_layoutList[2], 1, 2);
        gLayout->addWidget(m_layoutList[3], 2, 0);
        gLayout->addWidget(m_layoutList[4], 2, 1);
        gLayout->addWidget(m_layoutList[5], 2, 2);
        gLayout->setSpacing(0);

    }
    break;

    case NineType:
    {
        QGridLayout* gLayout = new QGridLayout(this);
        gLayout->setSpacing(0);


        for (int i = 0; i < 9; i++)
        {
            gLayout->addWidget(m_layoutList[i], i / 3, i % 3);
        }
    }
    break;
    default:
        break;
    }

}







